package com.lin.csdn.dao.impl;

import com.lin.csdn.dao.BaseDao;
import com.lin.csdn.dao.intf.IBlogDao;
import com.lin.csdn.model.vo.BlogVO;
import com.lin.csdn.model.vo.TddVO;

import java.util.ArrayList;
import java.util.List;

public class BlogDao extends BaseDao implements IBlogDao {
    @Override
    public Object search(String domain,int beginIndex,int pageSize) {
        List<Object> list = new ArrayList<>();
        Object data;
        list.add(domain);
        list.add("否");
        Class<BlogVO> cls= BlogVO.class;
        String sql="SELECT * FROM blog where domain=? and status=? LIMIT "+beginIndex+","+pageSize;
        data=find(sql,list,cls);
        return data;
    }

    //复杂搜索
    public Object seek(String target){
        List<Object> list=new ArrayList<>();
        Class<BlogVO> cls= BlogVO.class;
        String sql="SELECT * FROM blog where domain like \"%"+target+"%\" or title like \"%"+target+"%\" or description like \"%"+target+"%\" or username like \"%"+target+"%\"";
        return find(sql,list,cls);
    }

    //按照点赞排序
    public Object thumbOrder(){
        List<Object> list=new ArrayList<>();
        Class<BlogVO> cls=BlogVO.class;
        return find("SELECT * FROM blog ORDER BY praise DESC LIMIT 0,10",list,cls);
    }

    @Override
    public Object ownBlog(int userId,int beginIndex,int pageSize){
        List<Object> list=new ArrayList<>();
        Object data;
        list.add(userId);
        Class<BlogVO> cls= BlogVO.class;
        String sql="SELECT * FROM blog where userid=? LIMIT "+beginIndex+","+pageSize;
        data=find(sql,list,cls);
        return data;
    }

    @Override
    public Object idBlog(int blogId){
        List<Object> list=new ArrayList<>();
        Object data;
        list.add(blogId);
        Class<TddVO> cls=TddVO.class;
        data=find("SELECT * FROM blog where id=?",list,cls);
        return data;
    }

    @Override
    public int addBlog(String username,int userId,String date){
        List<Object> list=new ArrayList<>();
        int blogId;
        list.add(userId);
        list.add(username);
        list.add("");
        list.add(0);
        list.add(date);
        list.add("");
        list.add("");
        list.add("草稿");
        blogId=update("INSERT INTO blog (userid,username,domain,praise,date,title,description,status) VALUES (?,?,?,?,?,?,?,?)",list);
        return blogId;
    }

    public void addNewArticle(int blogId){
        List<Object> list=new ArrayList<>();
        list.add(blogId);
        list.add("G:\\WIS\\article\\"+blogId+".txt");
        update("INSERT INTO article (blogid,context) VALUES (?,?)",list);
    }

    public String getArticleId(int blogId){
        List<Object> list=new ArrayList<>();
        list.add(blogId);
        Object result=findSingle("SELECT id FROM article where blogid=?",list);
        if(result==null){
            return "404";
        }
        return String.valueOf(result);
    }

    @Override
    public String getUserId(int blogId){
        List<Object> list=new ArrayList<>();
        list.add(blogId);
        Object result=findSingle("SELECT userid FROM blog where id=?",list);
        return String.valueOf(result);
    }

    public void updateStatus(int blogId, String path){
        List<Object> list=new ArrayList<>();
        list.add("否");
        list.add(blogId);
        update("UPDATE blog SET status=? WHERE id=?",list);//更新博客状态为开放

        List<Object> AList=new ArrayList<>();
        AList.add(path);
        AList.add(0);
        AList.add(0);
        AList.add(blogId);
        update("INSERT INTO picture (picurl,blogid,reportid,shareid) VALUES (?,?,?,?)",AList);//将分享用的二维码相对路径放到数据库

    }

    public Object idAllBlog(int blogId){
        List<Object> list=new ArrayList<>();
        Object data;
        list.add(blogId);
        Class<BlogVO> cls= BlogVO.class;
        data=find("SELECT * FROM blog where id=?",list,cls);
        return data;
    }

    @Override
    public void deleteBlog(int blogId){
        List<Object> list=new ArrayList<>();
        list.add(blogId);
        update("Delete From blog where id=?",list);
    }

    public String loadThumb(int blogId){
        List<Object> list=new ArrayList<>();
        list.add(blogId);
        Object result=findSingle("SELECT praise FROM blog where id=?",list);
        return String.valueOf(result);
    }

    public void addThumb(int thumb,int blogId){
        List<Object> list=new ArrayList<>();
        list.add(thumb);
        list.add(blogId);
        update("UPDATE blog SET praise=? WHERE id=?",list);
    }

    public String searchTitle(int blogId){
        List<Object> list=new ArrayList<>();
        list.add(blogId);
        Object title=findSingle("SELECT title FROM blog where id=?",list);
        return String.valueOf(title);
    }
}
